define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, Template, Echarts) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/stock/index' + location.search,
                    // add_url: 'goods/goods/add',
                    // edit_url: 'goods/goods/edit',
                    // del_url: 'goods/goods/del',
                    // multi_url: 'goods/goods/multi',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'gid'},
                        {field: 'title', title: __('Title'),operate: 'LIKE'},
                        {field: 'place_origin', title: __('Place_origin')},
                        {field: 'number', title: __('Number')},
                        {field: 'unit', title: __('Unit'),operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                        {field: 'price', title: __('Price'), operate:'BETWEEN',operate: false},
                        // {field: 'new_cost_price', title: __('Cost'), operate:'BETWEEN'},
                        {field: 'specs', title: __('Specs'),operate: false},


                        {field: 'cate.name', title: __('Cate.name')},
                        {field: 'brands.name', title: __('Brands.name')},
                        {field: 'supplier.nickname', title: __('Supplier.nickname')},
                        {field: 'weigh', title: __('Weigh'),operate: false},
                        {field: 'status', title: __('上下架'),  searchList: {"normal":__('上架'),"hidden":__('下架')},formatter: Controller.api.formatter.custom},
                        {field: 'storehouse_id', title: __('所辖仓库'), visible: false,searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=7")},
                        // {field: 'storehouse_id', title: __('所辖仓库'),formatter: Controller.api.formatter.customs},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        //自定义栏位,custom是不存在的字段
                        // {
                        //     field: 'retention',
                        //     title: __('滞留时间'),
                        //     operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime
                        //     // searchList: Config.hslist,
                        //     // operate: false,
                        // },
                        {field: 'stock', title: __('Stock'),operate: false},
                        {field: 'weighting_cost', title: __('成本均价'),operate: false},
                        {field: 'new_cost', title: __('最新进价'),operate: false},
                        {field: 'balance', title: __('成本金额'),operate: false},
                        {field: 'cumulative', title: __('累计入库'),operate: false},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                // {
                                //     name: 'addpgoods',
                                //     title: __('采购'),
                                //     classname: 'btn btn-xs btn-success btn-click editstatus',
                                //     icon: 'fa fa-shopping-basket',
                                //     hidden:function(row){
                                //         // if(row.status != '审核'){
                                //         //     return true;
                                //         // }
                                //     },
                                //     click: function(data){
                                //         var gid = $(this).parents('tr').find('.gid').html()
                                //         layer.open({
                                //             type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                //             area: ['50%', '90%'],
                                //             title: "采购商品",
                                //             content: 'goods/addpgoods?ids='+gid,
                                //             // content: Config.this_url+'/purchase/requisition',
                                //         })
                                //     }},
                                {
                                    name: 'lists',
                                    title: __('采购记录'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-chevron-down',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var gid = $(this).parents('tr').find('.gid').html()
                                        var purl = 'goods/lists?ids='+gid;
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['50%', '90%'],
                                            title: "采购记录",
                                            content: purl,
                                            // content: Config.this_url+'/goods/lists?ids='+gid,
                                        })
                                    }},
                                {
                                    name: 'plists',
                                    title: __('销售记录'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-chevron-up',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var gid = $(this).parents('tr').find('.gid').html()
                                        var surl = Config.this_url+'/goods/goods/plists?ids='+gid;
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['50%', '90%'],
                                            title: "销售记录",
                                            content: surl,
                                            // content: Config.this_url+'/goods/lists?ids='+gid,
                                        })
                                    }}
                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $('.btn-monitor').click(function () {
                var idsarr = [];
                $('tbody').find('tr').each(function () {
                    if($(this).find('input').is(':checked')){
                        idsarr.push($(this).find('.gid').html())
                    }
                })
                if(idsarr.length > 0){
                    console.log(idsarr)
                }else{
                    layer.msg('未选中商品')
                }
                var url = Config.this_url+'/count/monitor/add?ids='+idsarr.join('-')
                // console.log(url)
                Layer.open({
                    type: 2,
                    title: __('设置商品预警'),
                    area: ["600px", "40%"],
                    content: url,
                });
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="goods/goods/change" data-id="' + row.id + '"><i class="fa ' + (row.status == 'hidden' ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="goods/goods/change" data-id="' + row.id + '"><i class="fa ' + (row.status == 'hidden' ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
        }
    };
    return Controller;
});